<!--  -->
<template>
  <div class="login">
    <div class="login1">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
    <!-- <div>
      <input type="text" id="code" /><button @click="getWxCode">
        获取code
      </button>
    </div>

    <div>
      <input type="text" id="ticket" /><button @click="getIscToken">
        获取票据
      </button>
    </div> -->

    <!-- <div>
      <input type="text" id="accessToken" /><button @click="getAccesstoken">
        获取Accesstoken
      </button>
    </div> -->
    <!-- <div>
      <input type="text" id="userinfo" /><button @click="getuserinfo">
        获取用户信息
      </button>
    </div> -->
  </div>
</template>

<script>
import axios from "axios";
import Vue from "vue";

import { Field, Toast, Uploader, Form } from "vant";
import login from "../../src/service/login.js";

Vue.use(Form);
Vue.use(Field);
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      paramValue: "",
      username: "",
      password: "",
    };
  },
  //监听属性 类似于data概念
  computed: {},
  created() {},
  // import  '../public/static/jquery-2.2.4.min.js'
  // import '../public/static/jweixin-1.2.0.js'
  // import '../public/static/iscpdemo.js'
  mounted() {
    // const s = document.createElement('script');
    // s.type = 'text/javascript';
    // s.src = '../../public/static/jquery-2.2.4.min.js';
    // document.body.appendChild(s);
    // const  b= document.createElement('script');
    // b.type = 'text/javascript';
    // b.src = '../../public/static/jweixin-1.2.0.js';
    // document.body.appendChild(b);
    // const c = document.createElement("script");
    // c.type = "text/javascript";
    // c.src = "../../public/static/iscpdemo.js";
    // document.body.appendChild(c);
  },
  //监控data中的数据变化
  watch: {},

  //方法集合
  methods: {
    // 输出参数值
    onSubmit(values) {
      var password = this.$crypto.encrypt(this.password);
      this.$store.commit("setusername", this.username);
      login
        .login1({
          userName: this.username,
          password: password,
        })

        .then((res) => {
    
          if (res) {
            this.$toast({
              message: "登录成功！",
              position: "center",
            });
            this.$store.commit("setUser", res);

            if (res) {
              this.$router.push({
                path: "/Home",
                query: {
                  name: "login",
                },
              });
            }
          } else {
            this.$toast({
              message: "登录失败用户名或密码错误！",
              position: "center",
            });
          }
        })
        .catch((error) => {
          this.$toast({
            message: "登录失败！",
            position: "center",
          });
        });
    },
    getWxCode() {
      var sURL = "https://igw.isgcc.net:18081/connect/oauth2/authorize?";
      sURL += "appid=ww445f8033443a14aa";
      sURL +=
        "&redirect_uri=" + encodeURIComponent("http://111.198.60.33:8888");
      sURL += "&response_type=code";
      sURL += "&scope=SCOPE";
      sURL += "&state=STATE";

      sURL += "&agentid=1008177";
      sURL += "#wechat_redirect";

      location.href = sURL;

      function getQueryString(name) {
        const url_string = location.href; // window.location.href
        const url = new URL(url_string);
        return url.searchParams.get(name);
      }

      this.paramValue = getQueryString("code");
      var codes = document.querySelector("#code");

      codes.value = this.paramValue;
    },
    getIscToken() {
      function getQueryString(name) {
        const url_string = location.href; // window.location.href
        const url = new URL(url_string);
        return url.searchParams.get(name);
      }

      this.paramValue = getQueryString("code");
      var codes = document.querySelector("#code");

      codes.value = this.paramValue;

      var params = {
        CODE: this.paramValue,
        appId: "74f1435fa45c4a03a4ab3cad77efd049",
      };
      // params = JSON. stringify(params )
      axios({
        method: "POST",
        url: "https://igw.isgcc.net:18443/identity/getAuthTicketByWechatCode",
        data: params,
      }).then((res) => {
        console.log(res);
        console.log(res.data);

        var ticket = document.querySelector("#ticket");
        ticket.value = res.data.data;
      }),
        (error) => {
          console.log("错误", error.message);
        };
    },
    // getAccesstoken() {
    //   var params = {
    //     code: paramValue,
    //   };
    //   axios({
    //     method: "POST",
    //     url: "https://igw.isgcc.net:18443/sgid-provider-province-identity/identity/getUsersByCode",
    //     data: params,
    //   }).then((res) => {
    //     if (res.code === "100001") {
    //       var accessToken = document.querySelector("#accessToken");
    //       accessToken.val = res.data.accessToken;
    //     }
    //   }),
    //     (error) => {
    //       console.log("错误", error.message);
    //     };
    //   // params = JSON. stringify(params)
    // },
  },

  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>
* {
  font-size: 16px;
}

.login {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(to bottom right, #c165dd, #5c27f1);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
}

/*@import url(); 引入公共css类*/
</style>
